<?php
namespace My\Controller;

use Utuo\BackgroundController;

class WechatActivityInviteController extends BackgroundController
{
    // init
    public function _initialize()
    {
        $this->setControllerTitle('微邀约');
        $this->setActionTitle(array(
            'index' => '邀约管理',
            'add' => '添加邀约',
            'recycle' => '回收站',
            'modify' => '修改邀约',
            'delete' => '删除邀约',
            'register' => '报名人员',
        ));
        
        $this->setActionNav('index,add,recycle');
    }
    
    
    // getList
    public function getLists($is_deleted)
    {
        $page = $this->getPage();
        $pagesize = $this->getPageSize();
        $d = D('WechatActivityInvite');
        $condition = array(
            'cid' => $this->aid,
            'is_deleted' => $is_deleted
        );
        $list = $d->where($condition)
            ->limit($pagesize)
            ->page($page)
            ->order('id DESC')
            ->select();
        $count = $d->where($condition)->count();
        foreach ($list as &$v) {
//             $v['activityUrl'] = $this->getUrl($this->aid, $v['id']);
            $v['activityUrl'] = uu($this->aid,'Home','Invite','index',array('cid'=>$this->aid,'aid'=>$v['id']),true);
        }
        $this->assign('itemList', $list);
        $this->showPage($count, $pagesize);
        $this->display();
    }
    
    /*
     * 查看发起活动情况
     */
    public function registerAction()
    {
        $id = I('id', 0, 'intval');
        $m = M('WechatActivityInviteRegister u');
        $m->join("LEFT JOIN __WECHAT_USER__ w ON w.openId = u.openId");
        $m->join("LEFT JOIN __WECHAT_ACTIVITY_INVITE_INVITER__ i ON u.inviterId=i.id");
        $m->where('u.cid=%d AND u.aid=%d', $this->aid, $id);
        $m->field('u.*,i.name AS inviterName,w.nickname,w.openId');
        $mCount = clone $m;
        $count = $mCount->count();
        $page = $this->getPage();
        $pagesize = 500;
        $lists = $m->limit($pagesize)
            ->page($page)
            ->order("id DESC")
            ->select();
        $this->assign('hostCount', $count);
        $this->assign('lists', $lists);
        $this->showPage($count, $pagesize);
        $this->display();
    }
    
    // 获取前台活动URL
    protected function getUrl($cid, $aid)
    {
        $url = U('Home/Invite/index', array(
            'cid' => $cid,
            'aid' => $aid
        ), true, true);
        return $url;
    }
}